package com.ja.openchat.adapters;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.allen.library.SuperButton;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.chat.PreviewWebViewActivity;
import com.ja.openchat.ui.found.CircleDetailActivity;
import com.ja.openchat.ui.found.CircleTopicActivity;
import com.ja.openchat.ui.found.OnCircleRemarkListener;
import com.ja.openchat.ui.found.OnPreviewMediaClickListener;
import com.ja.openchat.ui.found.PreviewCircleMediaActivity;
import com.ja.openchat.ui.me.MyAlbumFragment;
import com.ja.openchat.views.CustomCirclePopup;
import com.ja.openchat.views.LinkClickListener;
import com.ja.openchat.views.LinkTextViewMovementMethod;
import com.ja.openchat.views.UserAvatarView;
import com.ja.openchat.views.banner.Banner;
import com.ja.openchat.views.banner.ImageRoundHolderCreator;
import com.ja.openchat.views.banner.IndicatorView;
import com.ja.openchat.views.banner.OnPageItemClickListener;
import com.lqr.emoji.MoonUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.xiaoguang.selecttext.SelectTextHelper;
import com.zy.openchat.App;
import com.zy.openchat.Constants;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.circle.BlogInfo;
import com.zy.openchat.core.model.circle.BlogReplyInfo;
import com.zy.openchat.core.model.circle.BlogReplysInfo;
import com.zy.openchat.core.model.circle.BlogUserInfo;
import com.zy.openchat.core.model.circle.InputBlogIdType;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.core.model.video.VideoInfo;
import com.zy.openchat.util.Common;
import com.zy.openchat.util.FastClickUtil;
import com.zy.openchat.util.TimeUtils;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;

public class CircleHomeAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, CircleHomeViewHolder> implements LoadMoreModule ,SelectTextHelper.OnSelectListener{

    private final int selectedColor = ContextCompat.getColor(App.getInstanse(), R.color.circle_selected_color);
    private final int unselectedColor = ContextCompat.getColor(App.getInstanse(), R.color.circle_unselected_color);

    private boolean isDetail = false;
    private int listType = 0;
    private BaseViewModel viewModel;

    public CircleHomeAdapter(int type, List<MultiItemEntity> multiItemEntities, BaseViewModel viewModel) {
        super(multiItemEntities);
        this.viewModel = viewModel;
        this.isDetail = type > 2;
        listType = type;
        addItemType(0, R.layout.item_circle_post);
        addItemType(1, R.layout.item_circle_remart);
        addChildClickViewIds(R.id.remark_et, R.id.remark_btn, R.id.like_btn, R.id.focus_btn, R.id.comment_btn, R.id.delete_btn, R.id.player_container, R.id.linearLayout,R.id.post_content);

    }
    private SelectTextHelper mSelectableTextHelper = null;

    public void notifyLike(BlogReplyInfo replyInfo) {
        if (replyInfo == null || replyInfo.blog_id == null) {
            return;
        }
        for (int i = 0; i < getDefItemCount(); i++) {
            if (getItem(i) != null
                    && (getItem(i) instanceof BlogInfo)
                    && replyInfo.blog_id.blog_id == ((BlogInfo) getItem(i)).id) {
                BlogInfo blogInfo = (BlogInfo) getItem(i);
                if (replyInfo.liked == blogInfo.liked) {
                    break;
                }
                int likeNumber = replyInfo.liked ? blogInfo.like_count + 1 : blogInfo.like_count - 1;
                if (likeNumber < 0) {
                    return;
                }
                blogInfo.like_count = likeNumber;
                blogInfo.liked = replyInfo.liked;
                notifyItemChanged(i);
                break;
            }
        }
    }

    public void notifyLike(BlogInfo blogInfo) {
        if (blogInfo == null) {
            return;
        }
        for (int i = 0; i < getDefItemCount(); i++) {
            if (getItem(i) != null
                    && (getItem(i) instanceof BlogInfo)
                    && blogInfo.id == ((BlogInfo) getItem(i)).id) {
                BlogInfo current = (BlogInfo) getItem(i);
                current.like_count = blogInfo.like_count;
                current.liked = blogInfo.liked;
                notifyItemChanged(i);
                break;
            }
        }
    }

    public void removeBlog(long blogId) {
        for (int i = 0; i < getDefItemCount(); i++) {
            if (getItem(i) != null
                    && (getItem(i) instanceof BlogInfo)
                    && blogId == ((BlogInfo) getItem(i)).id) {
                remove(i);
                break;
            }
        }
    }

    public void notifyComment(BlogReplyInfo replyInfo) {
        if (replyInfo == null || replyInfo.blog_id == null) {
            return;
        }
        for (int i = 0; i < getDefItemCount(); i++) {
            if (getItem(i) != null
                    && (getItem(i) instanceof BlogInfo)
                    && replyInfo.blog_id.blog_id == ((BlogInfo) getItem(i)).id) {
                BlogInfo blogInfo = (BlogInfo) getItem(i);
                blogInfo.reply_count = blogInfo.reply_count + 1;
                notifyItemChanged(i);
                break;
            }
        }
    }

    public void updateBlogUserInfo(BlogUserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        blogUserInfoHashMap.put(userInfo.user_id, userInfo);
        for (int i = 0; i < getDefItemCount(); i++) {
            if (getItem(i) != null
                    && (getItem(i) instanceof BlogInfo)
                    && userInfo.user_id == ((BlogInfo) getItem(i)).user_id) {
                notifyItemChanged(i);
            }
        }
    }

    public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
        try {
            MultiItemEntity multiItemEntity = getItem(position);
            if (multiItemEntity == null) {
                return;
            }
            switch (view.getId()) {
                case R.id.linearLayout:
                    new XPopup.Builder(getContext())
                            .asCustom(new CustomCirclePopup(getContext()))
                            .show();
                    break;
                case R.id.player_container:
                    if (multiItemEntity instanceof BlogInfo) {
                        BlogInfo blogInfo = (BlogInfo) multiItemEntity;
                        if(onPreviewMediaClickListener!=null){
                            onPreviewMediaClickListener.onPreviewMediaClick(position,0);
                        }else{
                            PreviewCircleMediaActivity.previewMedia(getContext(), blogInfo, position);
                        }

                    }
                    break;
                case R.id.comment_btn:
                    if (multiItemEntity instanceof BlogInfo) {
                        BlogInfo blogInfo = (BlogInfo) multiItemEntity;
                        CircleDetailActivity.startDetail(getContext(), blogInfo.id, 1);
                    }
                    break;
                case R.id.like_btn:
                    if (FastClickUtil.isFastClick(view)) {
                        return;
                    }
                    if (multiItemEntity instanceof BlogReplyInfo) {
                        BlogReplyInfo replyInfo = (BlogReplyInfo) multiItemEntity;
                        invokeLike(new InputBlogIdType(false, replyInfo.id), !replyInfo.liked, position);
                    } else {
                        BlogInfo blogInfo = (BlogInfo) multiItemEntity;
                        invokeLike(new InputBlogIdType(true, blogInfo.id), !blogInfo.liked, position);
                    }
                    break;
                case R.id.focus_btn:
                    if (multiItemEntity instanceof BlogInfo) {
                        BlogInfo blogInfo = (BlogInfo) multiItemEntity;
                        BlogUserInfo blogUserInfo = requestBlogUserInfo(blogInfo.user_id);
                        if (blogUserInfo == null) {
                            return;
                        }
                        boolean followed = !blogUserInfo.followed;
                        if (followed) {
                            actionFollow(blogInfo, followed, position);
                        } else {
                            new XPopup.Builder(getContext()).autoDismiss(true)
                                    .asConfirm(getContext().getString(R.string.dialog_title_tip)
                                            , getContext().getString(R.string.unfollow_the_user_text)
                                            , getContext().getString(R.string.cancle_text)
                                            , getContext().getString(R.string.ok_text), new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    actionFollow(blogInfo, followed, position);
                                }
                            }, new OnCancelListener() {
                                @Override
                                public void onCancel() {

                                }
                            }, false).show();
                        }
                    }
            }
        } catch (Exception e) {

        }
    }


    private void actionFollow(BlogInfo blogInfo, boolean followed, int position) {
        showProgress();
        TelegramManager.getTelegramManager().followBlog(getContext(), blogInfo.user_id, followed, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (obj != null) {
                    if (obj.ok) {
                        MultiItemEntity multiItemEntity = getItem(position);
                        if (multiItemEntity instanceof BlogInfo) {
                            BlogInfo blogInfo1 = (BlogInfo) multiItemEntity;
                            BlogUserInfo blogUserInfo = requestBlogUserInfo(blogInfo1.user_id);
                            if (blogUserInfo != null) {
                                blogUserInfo.followed = followed;
//                                notifyBlogUserCache(blogUserInfo);
                                EventBus.getDefault().post(TgEvent.event(TelegramManager.EUser_TgEvent.ECircle_Follow_Update, blogUserInfo));
                            }
//                            updateBlogUserInfo(blogUserInfo);
                        }

//                        notifyItemChanged(position);
                    } else {
                        showErrorTip(obj.errString);
                    }
                } else {
                    showErrorTip(getContext().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getContext().getString(R.string.request_error));
            }
        });
    }


    @Override
    public MultiItemEntity getItem(int position) {
        try {
            return super.getItem(position);
        } catch (Exception e) {
            return null;
        }
    }

    private void invokeLike(InputBlogIdType inputBlogIdType, boolean isLike, int position) {
//        showProgress();
        TelegramManager.getTelegramManager().likeBlog(getContext(), inputBlogIdType, isLike, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (obj != null) {
                    if (obj.ok) {
                        MultiItemEntity multiItemEntity = getItem(position);
                        if (multiItemEntity instanceof BlogReplyInfo) {
                            BlogReplyInfo blogReplyInfo = (BlogReplyInfo) multiItemEntity;
                            blogReplyInfo.liked = isLike;
                            blogReplyInfo.like_count = isLike ? blogReplyInfo.like_count + 1 : blogReplyInfo.like_count - 1;
                            notifyItemChanged(position);
                        } else {
                            BlogInfo blogInfo = (BlogInfo) multiItemEntity;
                            blogInfo.liked = isLike;
                            blogInfo.like_count = isLike ? blogInfo.like_count + 1 : blogInfo.like_count - 1;
                            EventBus.getDefault().post(TgEvent.event(TelegramManager.EUser_TgEvent.ECircle_Like_Update, blogInfo));
                        }
                        TelegramManager.getTelegramManager().getCircleUnreadCount();
//                        notifyItemChanged(position);
                    } else {
                        showErrorTip(obj.errString);
                    }
                } else {
                    showErrorTip(getContext().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getContext().getString(R.string.request_error));
            }
        });
    }


    private void invokeInnerLike(CircleInnerRemarkAdapter adapter, long replyId, boolean isLike, int position) {
//        showProgress();
        TelegramManager.getTelegramManager().likeBlog(getContext(), new InputBlogIdType(false, replyId), isLike, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (obj != null) {
                    if (obj.ok) {
                        BlogReplyInfo blogReplyInfo = adapter.getItem(position);
                        blogReplyInfo.liked = isLike;
                        blogReplyInfo.like_count = isLike ? blogReplyInfo.like_count + 1 : blogReplyInfo.like_count - 1;
                        adapter.notifyItemChanged(position);
                    } else {
                        showErrorTip(obj.errString);
                    }
                } else {
                    showErrorTip(getContext().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getContext().getString(R.string.request_error));
            }
        });
    }

    private void deleteBlog(long blogId, int position) {
        showProgress();
        TelegramManager.getTelegramManager().deleteBlog(getContext(), blogId, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if (obj != null) {
                    if (obj.ok) {
//                        remove(position);
                        TelegramManager.getTelegramManager().getCircleUnreadCount();
                        if (getContext() instanceof CircleDetailActivity) {
                            ((CircleDetailActivity) getContext()).finish();
                        }
                    } else {
                        showErrorTip(obj.errString);
                    }
                } else {
                    showErrorTip(getContext().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip(getContext().getString(R.string.request_error));
            }
        });
    }

    private void showProgress() {
        if (getContext() != null && getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).showProgress();
        }
    }

    private void hideProgress() {
        if (getContext() != null && getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).hideProgress();
        }
    }

    private void showErrorTip(String message) {
        if (getContext() != null && getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).showErrorTip(message);
        }
    }

    public void cleanUserCache() {
        blogUserInfoHashMap.clear();
    }

    private HashMap<Long, BlogUserInfo> blogUserInfoHashMap = new HashMap<>();

    private BlogUserInfo requestBlogUserInfo(long userId) {
        if (blogUserInfoHashMap.containsKey(userId)) {
            return blogUserInfoHashMap.get(userId);
        } else {
            TelegramManager.getTelegramManager().getBlogUser(getContext(), userId, new TgResultBlock<BlogUserInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, BlogUserInfo obj) {
                    updateBlogUserInfo(obj);
                }

                @Override
                public void onTimeout(Map<String, Object> request) {

                }
            });
            return null;
        }
    }

    private void notifyBlogUserCache(BlogUserInfo blogUserInfo) {
        if (blogUserInfo == null) {
            return;
        }
        blogUserInfoHashMap.put(blogUserInfo.user_id, blogUserInfo);
    }
    private OnPreviewMediaClickListener onPreviewMediaClickListener;
    public void setPreviewMediaClickListener(OnPreviewMediaClickListener listener){
        this.onPreviewMediaClickListener = listener;
    }

    @Override
    protected void convert(@NotNull final CircleHomeViewHolder helper, MultiItemEntity itemEntity) {
        if (itemEntity.getItemType() == 0) {
            BlogInfo circleInfo = (BlogInfo) itemEntity;
            helper.setGone(R.id.zhidingTag,!circleInfo.pinned);
            UserInfo userInfo = UserInfo.getUserInfo(circleInfo.user_id);
            UserAvatarView iv_icon = helper.getView(R.id.headerImageView);
            if (userInfo != null) {
                if (userInfo.profile_photo != null && !userInfo.profile_photo.isSmallPhotoDownloaded()) {
                    circleInfo.isAvaterDownloaded = false;
                } else {
                    circleInfo.isAvaterDownloaded = true;
                }
                helper.setText(R.id.nick_name, userInfo.displayName());
                iv_icon.showUser(userInfo);
            } else {
                iv_icon.showDefault();
                helper.setText(R.id.nick_name, "");
                circleInfo.isAvaterDownloaded = false;
                TelegramManager.getTelegramManager().getUserSimpleInfo(circleInfo.user_id);
            }
            if (circleInfo.user_id == UserInfo.getUserInfo().id) {
                helper.setGone(R.id.focus_btn, true);
            } else {
                BlogUserInfo blogUserInfo = requestBlogUserInfo(circleInfo.user_id);
                if (blogUserInfo != null) {
                    if (listType == 2) {
                        helper.setGone(R.id.focus_btn, true);
                    } else {
                        helper.setGone(R.id.focus_btn, false);
                        SuperButton focus_btn = helper.getView(R.id.focus_btn);
                        if (blogUserInfo.followed) {
                            focus_btn.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                            focus_btn.setTextColor(getContext().getResources().getColor(R.color.me_nick_name_color_text_sub));
                            focus_btn.setShapeStrokeColor(getContext().getResources().getColor(R.color.color_f5f9fa));
                            focus_btn.setShapeSolidColor(getContext().getResources().getColor(R.color.color_f5f9fa)).setUseShape();
                            focus_btn.setText(R.string.focused_text);
                        } else {
                            focus_btn.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(), R.mipmap.follow_add), null, null, null);
                            focus_btn.setText(R.string.unfocus_text);
                            focus_btn.setTextColor(getContext().getResources().getColor(R.color.login_btn_bg_color));
                            focus_btn.setShapeStrokeColor(getContext().getResources().getColor(R.color.login_btn_bg_color));
                            focus_btn.setShapeSolidColor(getContext().getResources().getColor(R.color.white)).setUseShape();

                        }
                    }
                } else {
                    helper.setGone(R.id.focus_btn, true);
                }

            }/**/

            Banner banner = helper.getView(R.id.banner_view);
            IndicatorView indicatorView = helper.getView(R.id.indicator);

            helper.getView(R.id.headerImageView).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Bundle bundle = new Bundle();
                    bundle.putLong("userId", circleInfo.user_id);
                    viewModel.startContainerActivity(MyAlbumFragment.class.getCanonicalName(), bundle);

                }
            });
            if (UserInfo.getUserInfo().id == circleInfo.user_id) {
                helper.setGone(R.id.more_btn, true);
            } else {
                helper.setGone(R.id.more_btn, false);
                helper.getView(R.id.more_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new XPopup.Builder(getContext())
                                .hasShadowBg(false)
                                .atView(v)
                                .asAttachList(new String[]{getContext().getString(R.string.block_this_activity), getContext().getString(R.string.report_text)}
                                        , null,
                                        (position, text) -> {
                                            if (position == 0) {
                                                deleteBlog(circleInfo.id, helper.getAdapterPosition());
                                            } else {
                                                PreviewWebViewActivity.loadUrl(getContext(), getContext().getString(R.string.report_text), Constants.KHostEReport);
                                            }
                                        }).show();
                    }
                });
            }
            circleInfo.checkMediaDownload(getContext());
            if (circleInfo.isVideoType()) {
                VideoInfo videoInfo = circleInfo.content.video;
                indicatorView.setVisibility(View.GONE);
                banner.setVisibility(View.INVISIBLE);
                helper.player_container_wrapper.setVisibility(View.VISIBLE);
                RequestBuilder<Drawable> thumbnailRequest = null;
                if (videoInfo.thumbnail != null && videoInfo.thumbnail.isThumbnailDownloaded()) {
                    thumbnailRequest = Glide
                            .with(getContext())
                            .load(videoInfo.thumbnail.localPath());
                } else {
                    thumbnailRequest = Glide
                            .with(getContext())
                            .load(R.drawable.image_chat_placeholder_white);
                }
                Glide.with(getContext())
                        .load(videoInfo.localVideoPath())
                        .thumbnail(thumbnailRequest)
                        .into(helper.mThumb);
            } else {
                helper.player_container_wrapper.setVisibility(View.GONE);
                List<Object> urls = circleInfo.getMedioInfos();
                if (Common.isEmpty(urls)) {
                    indicatorView.setVisibility(View.GONE);
                    banner.setVisibility(View.GONE);
                } else {
                    indicatorView.setIndicatorColor(unselectedColor)
                            .setIndicatorSelectorColor(selectedColor)
                            .setIndicatorSpacing(4)
                            .setIndicatorRadius(2.5f);
                    banner.setIndicator(indicatorView, false)
                            .setAutoPlay(false)
                            .setHolderCreator(new ImageRoundHolderCreator());
                    banner.setOnPageClickListener(new OnPageItemClickListener() {
                        @Override
                        public void onPageItemClick(View view, int position) {
                            if(onPreviewMediaClickListener!=null){
                                onPreviewMediaClickListener.onPreviewMediaClick(helper.getAdapterPosition(),position);
                            }else{
                                PreviewCircleMediaActivity.previewMedia(getContext(), circleInfo, position);
                            }

                        }
                    });
                    indicatorView.setVisibility(View.VISIBLE);
                    banner.setVisibility(View.VISIBLE);
                    banner.setPages(urls);
                }
            }


            View line = helper.getView(R.id.line);
            TextView remark_et = helper.getView(R.id.remark_et);

            boolean isDetailAndMine = isDetail && UserInfo.getUserInfo().id == circleInfo.user_id;

            helper.setGone(R.id.delete_btn, !isDetailAndMine);
            helper.setGone(R.id.date_text, isDetailAndMine);
            TextView post_content = helper.getView(R.id.post_content);
            if (!TextUtils.isEmpty(circleInfo.text)) {

                MoonUtils.identifyFaceExpressionAndAt(getContext()
                        , post_content,circleInfo.text
                        , ImageSpan.ALIGN_BOTTOM
                        , circleInfo.entities==null?null:circleInfo.entities.entities);
                post_content.setMovementMethod(new LinkTextViewMovementMethod(new LinkClickListener() {
                    @Override
                    public boolean onLinkClick(String link) {
                        if(TextUtils.isEmpty(link)){
                            return true;
                        }
                        String[] array = link.split(":");
                        if(array!=null&&array.length>1){
                            if(link.startsWith("#")){
                                CircleTopicActivity.start(getContext(),array[1]);
                            }else if(link.startsWith("@")){
                                long userId = Long.parseLong(array[1]);
                                Bundle bundle = new Bundle();
                                bundle.putLong("userId", userId);
                                ((BaseActivity)getContext()).getViewModel().startContainerActivity(MyAlbumFragment.class.getCanonicalName(), bundle);
                            }
                        }

                        return true;
                    }

                    @Override
                    public void onLongLinkClick(String link) {

                    }
                }));
                post_content.setVisibility(View.VISIBLE);
                if(isDetail){
                    if(post_content.getTag()==null||!(post_content.getTag() instanceof SelectTextHelper)){
                        mSelectableTextHelper=new SelectTextHelper
                                .Builder(post_content)
                                .setCursorHandleColor(ContextCompat.getColor(getContext(),R.color.nav_btn_bg))
                                .setCursorHandleSizeInDp(24)
                                .setSelectedColor(ContextCompat.getColor(getContext(),R.color.nav_btn_bg_4d))
                                .setSelectAll(true)
                                .setScrollShow(true)
                                .setSelectedAllNoPop(false)
                                .setMagnifierShow(true)
                                .setPopSpanCount(5)
                                .setPopStyle(R.drawable.shape_color_4c4c4c_radius_8, R.drawable.ic_arrow)
                                .addItem(R.mipmap.ic_msg_copy, getContext().getString(R.string.copy_text), new SelectTextHelper.Builder.onSeparateItemClickListener() {
                                    @Override
                                    public void onClick() {
                                        copy(mSelectableTextHelper, selectedText);
                                    }
                                })
                                .build();
                        post_content.setTag(mSelectableTextHelper);
                    }else{
                        mSelectableTextHelper = (SelectTextHelper)post_content.getTag();
                    }
                    if(mSelectableTextHelper!=null){
                        mSelectableTextHelper.setSelectListener(this);
                    }
                }
            } else {
                post_content.setVisibility(View.GONE);
            }
            if (isDetail) {
                remark_et.setVisibility(View.VISIBLE);
                line.setVisibility(View.GONE);
                post_content.setMaxLines(Integer.MAX_VALUE);
            } else {
                remark_et.setVisibility(View.GONE);
                line.setVisibility(View.VISIBLE);
                post_content.setMaxLines(2);
            }
            helper.setText(R.id.like_number_text, String.format(getContext().getString(R.string.like_number_text), circleInfo.likeNumber()));
            helper.setText(R.id.comment_text, String.format(getContext().getString(R.string.comment_number_text), circleInfo.replyNumber()));
            helper.setText(R.id.location_text, circleInfo.location == null ? "" : circleInfo.location.address);
            helper.setText(R.id.date_text, TimeUtils.getMsgFormatTime(circleInfo.date * 1000));
            helper.getView(R.id.like_btn).setActivated(circleInfo.liked);
        } else {
            BlogReplyInfo blogReplyInfo = (BlogReplyInfo) itemEntity;
            UserInfo userInfo = UserInfo.getUserInfo(blogReplyInfo.user_id);
            UserAvatarView iv_icon = helper.getView(R.id.user_image);
            if (userInfo != null) {
                if (userInfo.profile_photo != null && !userInfo.profile_photo.isSmallPhotoDownloaded()) {
                    blogReplyInfo.isAvaterDownloaded = false;
                } else {
                    blogReplyInfo.isAvaterDownloaded = true;
                }
                helper.setText(R.id.user_name_text, userInfo.displayName());
                iv_icon.showUser(userInfo);
            } else {
                iv_icon.showDefault();
                helper.setText(R.id.user_name_text, "");
                blogReplyInfo.isAvaterDownloaded = false;
                TelegramManager.getTelegramManager().getUserSimpleInfo(blogReplyInfo.user_id);
            }
            helper.setText(R.id.post_time_text, TimeUtils.getMsgFormatTime(blogReplyInfo.date * 1000));
            helper.getView(R.id.like_btn).setActivated(blogReplyInfo.liked);
            helper.setText(R.id.like_btn, blogReplyInfo.likeNumber() == 0 ? "" : String.valueOf(blogReplyInfo.likeNumber()));
            helper.setText(R.id.remark_content, blogReplyInfo.text);
            helper.getView(R.id.user_image).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putLong("userId", blogReplyInfo.user_id);
                    viewModel.startContainerActivity(MyAlbumFragment.class.getCanonicalName(), bundle);

                }
            });
            RecyclerView recyclerView = helper.getView(R.id.recyclerView);
            TextView arrowBtn = helper.getView(R.id.arrowBtn);
            if (blogReplyInfo.reply_count > 0) {
                arrowBtn.setActivated(blogReplyInfo.isExpand);
                arrowBtn.setVisibility(View.VISIBLE);
                arrowBtn.setText(blogReplyInfo.isExpand ? getContext().getString(R.string.collapse_reply) : String.format(getContext().getString(R.string.expand_more_replies_format), blogReplyInfo.reply_count));
                if (blogReplyInfo.isExpand) {
                    recyclerView.setVisibility(View.VISIBLE);
                    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    CircleInnerRemarkAdapter adapter = new CircleInnerRemarkAdapter(blogReplyInfo.replys, blogReplyInfo, viewModel);
                    adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                        @Override
                        public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                            if (adapter != null && adapter instanceof CircleInnerRemarkAdapter) {
                                CircleInnerRemarkAdapter remarkAdapter = (CircleInnerRemarkAdapter) adapter;
                                BlogReplyInfo innerReplyInfo = remarkAdapter.getItem(position);
                                if (view.getId() == R.id.like_btn) {
                                    invokeInnerLike(remarkAdapter, innerReplyInfo.id, !innerReplyInfo.liked, position);
                                } else {
                                    if (getContext() instanceof OnCircleRemarkListener) {
                                        ((OnCircleRemarkListener) getContext()).showRemarkBar(new InputBlogIdType(false, innerReplyInfo.id), getContext().getString(R.string.comment_text), (View) view.getParent());
                                    }
                                }

                            }
                        }
                    });
                    recyclerView.setAdapter(adapter);
                } else {
                    recyclerView.setVisibility(View.GONE);
                }
                arrowBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        blogReplyInfo.isExpand = !blogReplyInfo.isExpand;
                        if (blogReplyInfo.isExpand && !blogReplyInfo.isRequestReplys) {
                            showProgress();
                            TelegramManager.getTelegramManager().getReplyReplys(getContext(), blogReplyInfo.id, blogReplyInfo.reply_count, 0, new TgResultBlock<BlogReplysInfo>() {
                                @Override
                                public void onResult(Map<String, Object> request, JsonObject response, BlogReplysInfo obj) {
                                    hideProgress();
                                    blogReplyInfo.isRequestReplys = true;
                                    blogReplyInfo.replys = obj == null ? null : obj.replys;
                                    notifyItemChanged(helper.getAdapterPosition());
                                }

                                @Override
                                public void onTimeout(Map<String, Object> request) {
                                    blogReplyInfo.isExpand = false;
                                    hideProgress();
                                    ToastUtils.showShort(getContext().getString(R.string.request_error));
                                }
                            });
                            return;
                        }
                        notifyItemChanged(helper.getAdapterPosition());
                    }
                });
            } else {
                recyclerView.setVisibility(View.GONE);
                arrowBtn.setVisibility(View.GONE);
            }
        }

    }
    private String selectedText;
    @Override
    public void onClick(View v) {

    }

    @Override
    public void onLongClick(View v) {

    }

    @Override
    public void onTextSelected(CharSequence content) {
        selectedText = content.toString();
    }

    @Override
    public void onDismiss() {

    }

    @Override
    public void onClickUrl(String url) {

    }

    @Override
    public void onSelectAllShowCustomPop() {

    }

    @Override
    public void onReset() {

    }

    @Override
    public void onDismissCustomPop() {

    }

    @Override
    public void onScrolling() {

    }

    private void copy(SelectTextHelper mSelectableTextHelper, String selectedText) {
        ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        if (cm != null) {
            cm.setPrimaryClip(ClipData.newPlainText(selectedText, selectedText));
        }
        if (null != mSelectableTextHelper) {
            mSelectableTextHelper.reset();
        }
        ToastUtils.showShort(getContext().getString(R.string.copied_text));
    }

    public int getRealItemCount() {
        return getDefItemCount();
    }

    @Override
    public @NotNull BaseLoadMoreModule addLoadMoreModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }
}
